const fs = require('fs')
const { Duplex, Readable } = require('stream');

const file = fs.createWriteStream('example.txt');

const inoutStream = new Duplex({
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    this.push(chunk);
    callback();
  },

  read(size) {
    this.push(String.fromCharCode(this.currentCharCode++));
    console.log('----------', size, this.currentCharCode)
    if (this.currentCharCode > 90) {
      this.push(null);
      inoutStream.end();
    }
  }
});

inoutStream.currentCharCode = 65;


const inStream = new Readable()

inStream.push('ABCDEFGHIJKLM');
inStream.push('NOPQRSTUVWXYZ');
inStream.push(null); // No more data


inStream.pipe(inoutStream).pipe(file);
